{% extends "base.html" %}

{% load i18n %}
{% load inventree_extras %}

{% block breadcrumb_list %}
{% endblock %}

{% block page_title %}
{% inventree_title %} | {% trans "Notifications" %}
{% endblock %}

{% block sidebar %}
    {% include "InvenTree/notifications/sidebar.html" %}
{% endblock %}

{% block content %}
    {% include "InvenTree/notifications/inbox.html" %}
    {% include "InvenTree/notifications/history.html" %}
{% endblock %}

{% block js_ready %}
{{ block.super }}

function updateNotificationTables() {
    $("#inbox-table").bootstrapTable('refresh');
    $("#history-table").bootstrapTable('refresh');
}
// this allows the global notification panel to update the tables
window.updateNotifications = updateNotificationTables


loadNotificationTable("#inbox-table", {
    name: 'inbox',
    url: '{% url 'api-notifications-list' %}',
    params: {
        read: false,
    },
    no_matches: function() { return '{% trans "No unread notifications found" %}'; },
});

$("#mark-all").on('click', function() {
    inventreeGet(
        '{% url "api-notifications-readall" %}',
        {
            read: false,
        },
        {
            success: function(response) {
                updateNotificationTables();
            }
        }
    );
});

loadNotificationTable("#history-table", {
    name: 'history',
    url: '{% url 'api-notifications-list' %}',
    no_matches: function() { return '{% trans "No notification history found" %}'; },
}, true);


$('#history-delete').click(function() {

    var html = `
    <div class='alert alert-block alert-danger'>
        {% trans "Delete all read notifications" %}
    </div>`;

    // Perform a bulk delete of all 'read' notifications for this user
    constructForm(
        '{% url "api-notifications-list" %}',
        {
            method: 'DELETE',
            multi_delete: true,
            preFormContent: html,
            title: '{% trans "Delete Notifications" %}',
            refreshTable: '#history-table',
            form_data: {
                filters: {
                    read: true,
                }
            }
        }
    );
});

$("#history-table").on('click', '.notification-delete', function() {
    constructForm(`{% url "api-notifications-list" %}${$(this).attr('pk')}/`, {
        method: 'DELETE',
        title: '{% trans "Delete Notification" %}',
        onSuccess: function(data) {
            updateNotificationTables();
        }
    });
});

enableSidebar('notifications');
{% endblock %}
